<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 获得菜单
 * @author yunyun
 */
class Index extends MY_Controller {
    
    var $controller_path 	    = '/menu/index/';
    var $model_path 		    = 'menu/menu_model';
    var $lang_path 			    = 'menu/menu';
    
	
    function __construct()
    {
		parent::__construct();
		log_message('debug', "menu/index Index controller Class Initialized.");
    }

    public function index()
    {
        $type = array(0,1,2);
        $menu_data_arr = $this->menu_model->get_alls_menu_tree($type,true);

        $this->api("200",$menu_data_arr,"");
    }

    public function save()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $post_data = $this->get_input("","array");
        if(!isset($post_data["parentId"])) $post_data["parentId"] = 0 ;
        //print_r($post_data);
        $this->form_validation->set_data($post_data);
        $this->form_validation->dir_set_rules('menu/menu');
        if($this->form_validation->run() == true) {

            if($post_data["id"]) $data_arr['id'] = $post_data["id"];

            $data_arr['name'] = $post_data["label"];
            $data_arr['type'] = $post_data["type"];
            $data_arr['parent_id'] = $post_data["parentId"];
            $data_arr['url'] = $post_data["url"];
            $data_arr['perms'] = $post_data["perms"];
            $data_arr['order_num'] = $post_data["orderNum"];
            $data_arr['icon'] = $post_data["icon"];

            $type = $this->menu_model->saves($data_arr);
            if($type) {
                $ret_data['type'] = 200;
                $ret_data['data'] = $type;
                $ret_data['message'] = $this->lang->line('menu_success');
            }
            else {
                $ret_data['message'] = $this->lang->line('menu_error');
            }
        }
        else {
            $ret_data['message'] = $this->form_validation->error_string();
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"菜单更新：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function delete()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $ids = $this->get_input();
        //print_r($ids);die();
        $ids_arr = NULL;
        $type = false;
        if($ids)
        {
            foreach($ids as $val)
            {
                $ids_arr[] = $val->id;
            }
            $type = $this->menu_model->dels($ids_arr);
        }


        if($type) {
            $ret_data['type'] = 200;
            $ret_data['message'] = $this->lang->line('menu_success');
        }
        else {
            $ret_data['message'] = $this->lang->line('menu_error');
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"菜单删除：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function get_nav_button()
    {
        //根据名字，查询权限
        $name = $this->input->get("userName");
        if($name == NULL) $this->api("304",NULL,"用户为空");
        $menu_arr = $this->_get_menu_ids($name);
        //log_message("debug",print_r($menu_arr,true));
        $type = array(1,2);

        $menu_data_arr = $this->menu_model->get_alls_menu($type,$menu_arr["isRoot"],$menu_arr["menu_ids_str"]);

        $ret_menu_arr = NULL;
        if($menu_data_arr)
        {
            foreach($menu_data_arr as $val)
            {
                $ret_menu_arr[] = $val->perms;
            }
        }

    	$this->api("200",$ret_menu_arr,"");
    }

    public function get_nav_tree()
    {
        //根据名字，查询权限
        $name = $this->input->get("userName");
        if($name == NULL) $this->api("304",NULL,"用户为空");
        $menu_arr = $this->_get_menu_ids($name);
        //log_message("debug",print_r($menu_arr,true));
        $type = array(0,1);

        $menu_data_arr = $this->menu_model->get_alls_menu_tree($type,$menu_arr["isRoot"],$menu_arr["menu_ids_str"]);
        
    	$this->api("200",$menu_data_arr,"");
    }

    
    /**
     * 获得用户权限组ID，获得菜单ID
     */
    private function _get_menu_ids($name = NULL)
    {
        $ret_arr["isRoot"] = false; //不是超级用户
        $ret_arr["menu_ids_str"] = NULL;

        $menu_ids_arr = NULL;

        $role_ids_str = NULL;

        $this->load->model("admin/admin_role_model");
        $admin_role_arr = $this->admin_role_model->get_all($name);
        if(!$admin_role_arr) $this->api("304",NULL,"角色信息为空");
        
        foreach($admin_role_arr as $val)
        {
            if($val->isroot == 1)
            {
                $ret_arr["isRoot"] = true;
                break;
            }


            $role_ids_str []= $val->role_id;
        }

        /**
         * 检测是不是超级用户
         * 超级用户不用管 role_menu
         * 普通用户组 要检测role_menu 确定权限。
         */
        $this->load->model("role/role_menu_model");
        if($ret_arr["isRoot"] == false)
        {
            //查找一下有多少 menu_id
            //$role_ids_str = trim($role_ids_str,",");
            $menu_ids_arr = $this->role_menu_model->get_menu_id_by_role_ids($role_ids_str);

            if($menu_ids_arr)
            {
                foreach($menu_ids_arr as $val)
                {
                    $ret_arr["menu_ids_str"][]= $val->menu_id;
                }
            }

            //$ret_arr["menu_ids_str"] = trim($ret_arr["menu_ids_str"],",");
        }
        
        return $ret_arr;
    }

}
